package com.example.javabasic.thread.classone.demo5;

/**
 * 捕获线程异常
 */
public class ThreadException {

    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            long a = 10L;
            long b = 0;
            try{
                long c = a/b;
            }catch(Exception e){
                throw e;
            }

        });
        Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                System.out.println(t);
                System.out.println("---------------");
                System.out.println(e);
                throw new RuntimeException(e);
            }
        };
//        thread.setUncaughtExceptionHandler((t,e)->{
//            System.out.println(t);
//            System.out.println("---------------");
//            System.out.println(e);
//        });
        thread.setUncaughtExceptionHandler(handler);
        thread.start();

    }
}
